import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;
        that.type = 1;

    }

    afterInstall() {
    }

    /** 设置显示哪个弹出框,可传入数据,格式json
     *  1 ==>  提示去登录框
     *  2 ==>  提示完善资料
     *  3 ==>  竞拍成功 {minusCoin: 'n个(30%)'}
     *  4 ==>  拟出局
     *  5 ==>  虚拟订单1被锁定
     *  6 ==>  联系客户时申明来自万屏汇
     *  7 ==>  敏感录音
     *  8 ==>  扣币提示
     *  9 ==>  项目不存在
     *  10 ==>  设置业务范围
     *  11 ==>  额度不足
     *
     * */

    setType(type, data) {
        let that = this;
        this.type = type;
        this.data = data;
        let dom;
        switch (type) {
            case 1 :
                dom = '<h4 class="title">请登录</h4>\n' +
                    '  <p class="info">您尚未登录，需要登录才能继续使用哦！</p>\n' +
                    '  <button class="btn btn-primary submit-btn" evt="toLoginPage">立即登录</button>';
                break;
            case 2 :
                dom = '<h4 class="title">完善个人资料</h4>\n' +
                    '  <p class="info">请及时完善您的个人资料，以免影响您的<br/>正常使用。</p>\n' +
                    '  <button class="btn btn-primary submit-btn" evt="toJoinPage">立即完善</button>';
                break;
            case 3 :
                dom = '<h4 class="title title-no-margin">竞拍成功</h4>\n' +
                    '<div class="icon-checkmark"></div>' +
                    '  <p class="info">请在15分钟内与客户联系<br>逾期名额作废并扣除' + data.minusCoin + '汇币</p>\n' +
                    '  <button class="btn btn-primary submit-btn" evt="goback">知道了</button>';
                break;
            case 4 :
                dom = '<h4 class="title title-no-margin">拟出局</h4>\n' +
                    '<div class="icon-x"></div>' +
                    '  <p class="info">若领先者放弃联系客户<br>您将有机会获得客户信息</p>\n' +
                    '  <button class="btn btn-primary submit-btn" evt="goback">知道了</button>';
                break;
            case 5 :
                dom = '<h4 class="title">已被其他金牌会员锁定</h4>\n' +
                    '  <p class="info">订单会优先通过微信公众号“万屏<br> 汇”推送！</p>\n' +
                    '  <a type="button" href="/#index" class="btn btn-primary submit-btn">知道了</a>';

                that.afterUninstall = function () {
                    location.hash = 'index';
                };
                break;
            case 6 :
                dom = '<h4 class="title title-no-info">联系客户时申明来自万屏汇，<br>可提高成单率哦！</h4>\n' +
                    '  <a type="button" href="tel:' + data.phone + '" class="btn btn-primary submit-btn">知道了</a>';
                break;
            case 7 :
                dom = '<h4 class="title title-no-info">该录音包含详细地址等敏感信息<br>抢单成功后可播放</h4>\n' +
                    '  <a type="button" href="/#index" class="btn btn-primary submit-btn">知道了</a>';
                break;
            case 8 :
                dom = '<h4 class="title">将扣取' + data.coin + '个汇币</h4>\n' +
                    '  <p class="info">竞拍成功后，可获得客户信息并成功扣除汇币<br>' +
                    '<a class="link">当前剩余' + data.point + '个，<span class="underline" evt="loadMyPack">立即充值</span></a></p>\n' +
                    '  <a type="button" class="btn btn-primary submit-btn min-margin" evt="grab">继 续</a>' +
                    '<p class="agreement-info">点击“继续”表示您已同意<span evt="openAgreement" class="text-primary">《万屏汇用户协议》</span></p>';
                break;
            case 9 :
                dom = '<h4 class="title">无法查看</h4>\n' +
                    '<img class="sade-img" src="/img/demand/sade.png"/>' +
                    '  <p class="info">该项目需要更高等级才能查看或者<br>可能已被删除</p>' +
                    '  <a type="button" href="/#index" class="btn btn-primary submit-btn" evt="backIndex">查看其他项目</a>';

                that.afterUninstall = function () {
                    location.hash = 'index';
                };
                break;
            case 10 :
                dom = '<h4 class="title">设置业务范围</h4>\n' +
                    '  <p class="info">设置您关注的业务范围，方便我们推送您<br>需要的订单信息</p>\n' +
                    '  <button class="btn btn-primary submit-btn" evt="toSetServicePage">立即设置</button>';
                break;
            case 11 :
                dom = '<h4 class="title">额度不足</h4>\n' +
                    '  <p class="info">您的汇币已不足，不能参与抢单<br>请立即充值</p>\n' +
                    '  <a type="button" class="btn btn-primary submit-btn half-btn" evt="goback">取消</a>' +
                    '  <a type="button" class="btn btn-primary submit-btn half-btn" evt="loadMyPack">充值</a>';
                break;
        }
        this.find('.content').html(dom);
        this.showModal();
    }

    showModal() {
        let content = this.config.parent.find('.CenterModalContent');
        let height = content.height();
        content.css('margin-top', -height / 2 + 'px');
        setTimeout(function () {
            content.css({'transform': 'scale(1,1)', 'opacity': '1'});
        }, 100);
    }

    // case 1
    _evt_toLoginPage() {

    }

    // case 2
    _evt_toJoinPage() {

    }

    // case 8   调用抢单接口
    _evt_grab() {

        let that = this;
        let data = globalData;
        let demand_id = globalData.current_demand.id;
        if (globalData.current_demand.tag == 2) {
            //TO2
            // tomato.getVPresenter<tomato.VPresenter>(funs.moduleToUrl('m/grab/GrabFailModal'), (GrabFailModal) => {
            //     tdom.open(GrabFailModal, tdom.DialogTarget.Blank, {
            //         size: { width: '90%', height: tomato.DialogSize.Content },
            //         effect: tdom.DialogEffect.scale,
            //         masked: true
            //     });
            // });
            return;
        }
        if (globalData.current_demand.tag == 1) {
            //TO1
            that.setType(5);
            return;
        }
        //判断套餐是否过期
        let now = new Date().getTime();
        let endDate = new Date(globalData.pack.end_date).getTime();
        let dayTransMs = 1000 * 60 * 60 * 24;//上一行代码得出的结果是截止日期当天的0点0分，所以需要在此基础上加24小时；
        //查看余额是否充足 && (now <= endDate + dayTransMs)
        if (parseInt(data.pack.bid_point) >= parseInt(that.data.coin)) {
            //抢单
            api.bidDemand({"id": demand_id, offer: that.data.coin}, {
                succ: function (json) {
                    that._evt_goback();

                    // 更新项目信息
                    let curr_pro = globalData.current_demand;
                    curr_pro.supplier_partic_status = json.demand_info.supplier_partic_status;
                    curr_pro.bid_cnt = json.demand_info.demand.bid_cnt;
                    curr_pro.status = json.demand_info.demand.status;
                    curr_pro.user.phone = json.demand_info.user.mobile;
                    curr_pro.audio = json.demand_info.demand.audio;
                    curr_pro.my_offer = parseInt(json.demand_info.my_offer);
                    if (curr_pro.my_offer >= 200) {
                        curr_pro.self_has_max = true;
                    } else {
                        curr_pro.self_has_max = false;
                    }
                    let pro_list = globalData.project_list;
                    // 更新项目列表中此项目的信息
                    for (let i = 0; i < pro_list.length; i++) {
                        if (curr_pro.id == pro_list[i].id) {
                            pro_list[i] = curr_pro;
                            break;
                        }
                    }
                    // 更新参与项目列表信息
                    let joined_list = globalData.joined_list;
                    let hasPro = false;
                    for (let i = 0; i < joined_list.length; i++) {
                        if (curr_pro.id == joined_list[i]) {
                            joined_list[i] = $.extend(joined_list[i], curr_pro);
                            hasPro = true;
                        }
                    }
                    if (!hasPro) {
                        joined_list.push(curr_pro);
                    }
                    if (json.demand_info.supplier_partic_status == 10 && json.demand_info.demand.status == 10) {
                        //首次出价
                        funs.tip('出价成功', '请保持领先状态，以免出局', 'succ');
                    } else if (json.demand_info.supplier_partic_status == 10 && (json.demand_info.demand.status == 20 || json.demand_info.demand.status == 30)) {
                        //二次进价时，或免费竞价中
                        funs.tip("出价成功", "若领先者放弃联系客户，您将有机会获得客户信息", "succ");
                    } else if (json.demand_info.supplier_partic_status == 100 || json.demand_info.supplier_partic_status == 120) {
                        //竞价成功弹窗

                        // 显示电话是虚拟号的提示信息
                    }

                    // 更新剩余次数
                    api.getMyPack(function (json) {
                        globalData.pack.bid_point = json.pack.bid_point;
                    });
                    // 获取项目参与列表
                    api.getParticInfo({demand_id: globalData.current_demand.id}, function (json) {
                        let list = json.partic_supplier_list;
                        globalData.current_demand.supplier_list.length = 0;
                        globalData.current_demand.supplier_list.push(...list);
                    });
                },
                fail: function (data) {
                    if ((data.status == '41028002') || (data.status == '41028003') || (data.status == '41028022')) {
                        //额度不足弹窗
                        that.setType(11);
                    } else {
                        that._evt_goback();
                        funs.tip("竞价失败", data.msg, 'fail');
                    }
                }
            });
        } else {
            ///额度不足弹窗
            that.setType(11);
        }
    }

    _evt_loadMyPack() {
        this._evt_goback();
        setTimeout(function () {
            location.hash = 'pack/MyPack';
        }, 310);
    }

    _evt_openAgreement() {

    }

    // case 9
    _evt_backIndex() {
        this._evt_goback();
        setTimeout(function () {
            location.hash = 'index';
        }, 310);
    }

    // 10
    _evt_toSetServicePage() {

    }
}
